﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using DAL;
using Model;
using Common;
using System.IO;
using System.Xml;

namespace BLL
{
    /// <summary>
    /// 现金红包
    /// </summary>
    public class SendRedPackBLL
    {
        /// <summary>
        /// 发送红包
        /// </summary>
        /// <returns></returns>
        public ReturnMessages Send(string openid)
        {
            ReturnMessages rm = new ReturnMessages();
            try
            {
                //获取配置信息
                XmlNodeList xmllist = XmlHelper.ReadNodes(Directory.GetCurrentDirectory() + "/wwwroot/config/weixin.config", "configuration");
                string appid = xmllist[0].InnerText;
                string mchid = xmllist[2].InnerText;
                string apikey = xmllist[3].InnerText;
                string sslcert_path = xmllist[4].InnerText;
                string sslcert_password = xmllist[5].InnerText;
                string server_ip = xmllist[6].InnerText;

                sendredpack_post model_post = new sendredpack_post();
                model_post.wxappid = appid;
                model_post.client_ip = server_ip;
                model_post.nonce_str = Utils.GetCheckCode(32);
                model_post.mch_id = mchid;
                model_post.mch_billno = mchid + DateTime.Now.ToString("yyMMddHHmmss") + Utils.Number(4);
                model_post.re_openid = openid;
                model_post.remark = "发送红包";
                model_post.act_name = "红包测试";
                model_post.send_name = "塔罗科技";
                model_post.total_amount = 100;
                model_post.total_num = 1;
                model_post.wishing = "恭喜发财";
                model_post.sign =WeiXinHelper.MakeSign<sendredpack_post>(model_post, apikey);

                string xml = HttpServiceHelper.Post_cert(XmlHelper.Serializer_WeiXin<sendredpack_post>(model_post),"https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", sslcert_path, sslcert_password).Result;
                sendredpack_return model_return = XmlHelper.DeserializeXmlToObject_WeiXin<sendredpack_return>(xml);

                if(model_return.return_code== "SUCCESS")
                {
                    if(model_return.result_code== "SUCCESS")
                    {
                        rm.code = 200;
                    }
                    else
                    {
                        LogHelper.Error("红包支付失败,错误代码："+ model_return.err_code, model_return.err_code_des);
                        rm.code = 400;
                        rm.msg = model_return.err_code_des;
                    }
                }
                else
                {
                    LogHelper.Error("红包发送失败", model_return.return_msg);
                    rm.code = 400;
                    rm.msg = model_return.return_msg;
                }

                
                return rm;
            }
            catch (Exception e)
            {
                rm.code = 500;
                rm.msg = "server error";
                LogHelper.Error(this.GetType().FullName, e.ToString());
                return rm;
            }
        }

        


    }
}
